<template>
  <section class="input">
    <el-input v-model="value" :type="type" :disabled="myDisabled" :placeholder="placeholder" :maxlength="max" :minlength="min" :readonly="readonly" @input="handleInput" @change="handleChange" />
  </section>
</template>
<script>
import mixins from './../mixins'
export default {
  name: 'VInput',
  componenttype: 'field',
  mixins: [mixins],
  props: {
    option: {
      type: Object,
      default: {
        type: 'text',
        value: '',
        placeholder: '',
        disabled: false,
        required: false,
        max: 200,
        min: 0,
        regx: '',
        msg: '',
        readonly: false,
        checker: null
      }
    }
  },
  data(){
    return{
      myDisabled:false
    }
  },
  computed: {
    type() {
      return this.option.type || 'text'
    },
    _error() {
      return this.error()
    },
    _error_message() {
      return this.error_message()
    }
  },
  created() {
    this.myDisabled=this.option.disabled || false
    /* this.on_event('radio-value-change', (e) => {
      if (e.target.name === 'fixedParam') {
        this.dispatch_event('filed-visible', e.data.target.option.name === 'paramType' && e.data.target.getValue() === '1')
      }
    })*/
  },
  methods: {

    setCheck() {
      let { value, required, max, min, regx, checker, msg } = this
      let error = false; let errorMsg = ''
      if (value !== undefined && value !== null) {
        value = value + ''
      }
      if (required && !value) {
        error = true
        errorMsg = '必填选项不能为空'
      }
      if (!error) {
        if (required || value) {
          if (!error && regx) {
            if (!new RegExp(regx).test(value)) {
              error = true
              errorMsg = msg
            }
          }
          if (!error) {
            min = +min
            max = +max
            if (min < 0) {
              min = 0
            }
            if (max < 0) {
              max = 0
            }
            if (min > max) {
              min = max
            }
            if (!(value.length >= min && value.length <= max)) {
              error = true
              errorMsg = `长度应该在${min}和${max}之间`
            }
          }
          if (!error && checker) {
            const { result, msg } = this.checker(value)
            error = result
            if (!error) {
              errorMsg = msg
            }
          }
        }
      }
      this.error_ = error
      this.error_message_ = errorMsg
      this.fieldCheck()
    },

    getValue() {
      return this.value
    },

    setValue(k) {
      this.$set(this, 'value', k)
    },

    handleInput(e) {
      this.dispatch_event('filed-value-change', e)
    },

    handleChange() {
      this.setCheck()
    }
  }
}
</script>
